package MVC.Views;

import MVC.Controller;
import MVC.View;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ApplicationView extends View implements ActionListener {

    /* Atributos */

    private JPanel mainPanel;
    private JTextField nomeTextField;
    private JButton conectarButton;
    private JLabel pentagoLabel;
    private JLabel nomeLabel;
    private JTextField servidorTextField;
    private JLabel servidorLabel;
    private JMenuItem sairMenuItem;

    public ApplicationView() {
        conectarButton.addActionListener(this);
        conectarButton.setActionCommand(":conectar");

        sairMenuItem = new JMenuItem("Sair");
        sairMenuItem.addActionListener(this);
        sairMenuItem.setActionCommand(":sair");

        open();
    }

    /* Métodos */
    public void rendered() {
        frame.setSize(new Dimension(300, 400));

        jogoMenu.add(sairMenuItem);

        refresh();
        show();
    }

    public void refresh() {

    }

    public JPanel mainPanel() {
        return mainPanel;
    }

    public void actionPerformed(ActionEvent actionEvent) {
        String action = actionEvent.getActionCommand();

        if(action.equals(":conectar")) {
            conectar();
        } else if(action.equals(":sair")) {
            sair();
        }
    }

    public void conectar() {
        if(!nomeTextField.getText().isEmpty() && !servidorTextField.getText().isEmpty()) {
            Controller.jogadorController.conectar(nomeTextField.getText(), servidorTextField.getText());
        } else
            alert("Preencha todos os campos antes de conectar.");
    }

    public void sair() {
        quit();
    }

}
